<template>
  <div class="w100p h100p bgfff pt15p">
    <div class="w100p h231  textc">
      <div>
        <img src="https://one-stand.oss-cn-shanghai.aliyuncs.com/product-index/20190420144409.png" alt="" class="w60 h60 mb15" />
        <p class="fs14 c78">亲爱的用户，您好</p>
        <p class="fs14 c78 pb27">进入小程序请先登录</p>


        <div class="pl20 pr21 pt30">

          <!--获取手机号-->
<!--          <button open-type="getPhoneNumber"-->
<!--                  class="bggreen bradius5 w100p lh44 textc cfff fs18"-->
<!--                  @getphonenumber="getPhoneNumber"-->
<!--                  v-if="!phoneMsg.phoneData"-->
<!--          >微信授权</button>-->

          <!--微信授权-->
          <button open-type="getUserInfo"
                  @getuserinfo="GetUserInfo"
                  class="bggreen bradius5 w100p lh44 textc cfff fs18"
          >登录</button>

        </div>
      </div>

    </div>

  </div>
</template>

<script>
  import WXAJAX from '../../utils/request'
  import websocket from '../../utils/websocket'
  import { mapActions } from 'vuex';
  import StaticResources from '@/components/StaticResources';

  export default {
    name: "login",
    props:[],
    components: {StaticResources},
    data(){
      return{
        islogin:true ,//是否已经登录
        wxCode:'',
        loginMsg:{
          rawData : '' ,
          signature : '' ,
          encryptedData : '' ,
          iv : '' ,
        },
        phoneMsg:{
          phoneData:'',
          iv:'',
        }
      }
    },
    mounted(){
      wx.setNavigationBarTitle({
        title: "登录"
      });
      if (!wx.getStorageSync('token')) {
        this.getLoginStatus();
      }

      /*   this.phoneMsg.phoneData = wx.getStorageSync('PHONEDATA') || '' ;
         this.phoneMsg.iv = wx.getStorageSync('PHONEIV') || '' ;*/
    },
    methods:{
      ...mapActions(['setCurrentCompany']),
      getPhoneNumber(e){
        this.phoneMsg.phoneData = e.target.encryptedData ;
        this.phoneMsg.iv = e.target.iv ;
        // wx.setStorageSync('PHONEDATA',this.phoneMsg.phoneData);
        // wx.setStorageSync('PHONEIV',this.phoneMsg.iv);
      },
      GetUserInfo(msg){//获取用户信息
        msg = msg.target;
        this.islogin = true ;
        this.loginMsg = {
          rawData : msg.rawData ,
          signature : msg.signature ,
          encryptedData : msg.encryptedData ,
          iv : msg.iv ,
        };
        this.setCurrentCompany({});
        this.login() ;
        // this.getLoginStatus();
        // this.login(msg) ;
      },
      getLoginStatus(){
        let v = this ;
        wx.login({
          success: (data) => {
            wx.hideLoading();
            if (data.code) {
              //保存code
              v.wxCode = data.code;
            } else {
              wx.showToast({
                title: '获取登录态失败！',
                duration: 2000,
                icon: 'none'
              });
              // console.log('获取用户登录态失败！' + data.errMsg)
            }
          },
          fail(res) {
            wx.hideLoading() ;
          }
        });
      },
      login(msg){
        wx.showLoading() ;
        WXAJAX.POST({
          code: this.wxCode,
          rawData:this.loginMsg.rawData,
          signature : this.loginMsg.signature,
          encrypteData : this.loginMsg.encryptedData,
          iv:this.loginMsg.iv,
          phoneData:this.phoneMsg.phoneData ,
          phoneIv : this.phoneMsg.iv ,
        },'','/home/wxLogin').then((data) => {
          if(data.token){
            wx.setStorageSync('token', data.token);
            wx.setStorageSync('isBoss' , data.roleId != 0 ) ;
            wx.setStorageSync('avatarUrl' , data.avatarUrl) ;
            wx.setStorageSync('userId', data.userId);
            wx.setStorageSync('userName', data.userName);
            wx.setStorageSync('ossUrl', data.ossUrl);
            wx.setStorageSync('phone', data.phone);
            websocket.connect(101);
            let url = wx.getStorageSync('redirectPage');
            if(url){
              setTimeout(()=>{
                wx.removeStorageSync('redirectPage');
                wx.reLaunch({url: url});
              }, 1*1000);
            }else{
              wx.switchTab({url:'../index/main'});
            }
          }
          wx.setStorageSync('loginMsg',this.loginMsg)
        }).catch((err) => {
          this.getLoginStatus();
          wx.hideLoading();
        });
      },
    }
  }
</script>

<style>

</style>


































